package org.xxpay.manage.order.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.util.DateUtil;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.AppNotify;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.CommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/app_notify")
public class AppNotifyController extends BaseController {

    private static final MyLog _log = MyLog.getLog(AppNotifyController.class);

    /**
     * APP通知记录列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Integer page = getInteger(param, "page");
        Integer limit = getInteger(param, "limit");
        // 订单起止时间
        Date createTimeStart = null;
        Date createTimeEnd = null;
        String createTimeStartStr = getString(param, "createTimeStart");
        if(StringUtils.isNotBlank(createTimeStartStr)) createTimeStart = DateUtil.str2date(createTimeStartStr);
        String createTimeEndStr = getString(param, "createTimeEnd");
        if(StringUtils.isNotBlank(createTimeEndStr)) createTimeEnd = DateUtil.str2date(createTimeEndStr);

        AppNotify appNotify = getObject(param, AppNotify.class);
        int count = commonService.appNotifyService.count(appNotify, createTimeStart, createTimeEnd);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<AppNotify> appNotifyList = commonService.appNotifyService.select(
                (getPageIndex(page) -1) * getPageSize(limit), getPageSize(limit), appNotify, createTimeStart, createTimeEnd);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(appNotifyList, count));
    }


    /**
     * 删除通知
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity<?> delete(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String str = request.getParameter("pollParam");
        List<Long> list = new ArrayList<>();
        //批量删除
        if (str != null){
            JSONArray jsonArray = JSON.parseArray(str);
            for(int i =0;i<jsonArray.size();i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id =jsonObject.get("id").toString();
                list.add(Long.valueOf(id));
            }
        Boolean result = commonService.appNotifyService.deleteByIds(list);
        if(result == true) return ResponseEntity.ok(BizResponse.buildSuccess());
        }
        //单个删除
        Long id = Long.valueOf(getStringRequired(param, "id"));
        list.add(id);
        Boolean result = commonService.appNotifyService.deleteByIds(list);
        if(result == true) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /**
     * 修改匹配状态
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        AppNotify appNotify = getObject(param, AppNotify.class);
        AppNotify notify = commonService.appNotifyService.find(appNotify);
        if (notify == null ) return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        if (notify.getStatus().equals(Constant.MATE_NO)){
            notify.setStatus(Constant.MATE_YES);
        }else {
            notify.setStatus(Constant.MATE_NO);
        }
        int count = commonService.appNotifyService.update(notify);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

}
